using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIActivity : MonoBehaviour
{
	public const string ITEM_PREFAB = "UI/ActivityNode";

	public Image imgBarBg;

	public Image imgBar;

	public Image imgCur;

	public Text txtLeftTime;

	public Text txtCurStar;

	public GameObject prizeWnd;

	public GameObject takePrizeWndBtn;

	public GameObject prizeWndTxt;

	private float _width;

	private UIActivityPrize[] _activityPrize;

	private List<UIActivityNode> _nodes = new List<UIActivityNode>();

	private int _curSelectId = -1;

	private void Start()
	{
		Vector2 sizeDelta = imgBar.rectTransform.sizeDelta;
		_width = sizeDelta.x;
		List<ActivityPrize> prizeList = ActivityManager.instance.prizeList;
		GameObject original = Resources.Load("UI/ActivityNode") as GameObject;
		_nodes.Clear();
		for (int i = 0; i < prizeList.Count; i++)
		{
			ActivityPrize activityPrize = prizeList[i];
			GameObject gameObject = UnityEngine.Object.Instantiate(original);
			gameObject.transform.parent = imgBarBg.transform;
			UIActivityNode component = gameObject.GetComponent<UIActivityNode>();
			_nodes.Add(component);
			component.id = activityPrize.id;
			component.parentUI = this;
			component.position = new Vector2(activityPrize.percent * _width, 115f);
			component.star.text = activityPrize.star.ToString();
		}
		_activityPrize = prizeWnd.GetComponentsInChildren<UIActivityPrize>(includeInactive: true);
		InvokeRepeating("CountDown", 0f, 1f);
	}

	private void Update()
	{
	}

	private void OnEnable()
	{
		imgBar.fillAmount = ActivityManager.instance.GetStarPercent();
		Vector2 sizeDelta = imgBar.rectTransform.sizeDelta;
		_width = sizeDelta.x;
		imgCur.rectTransform.anchoredPosition = new Vector2(imgBar.fillAmount * _width, -75f);
		txtCurStar.text = ConstantData.missionRecords.GetTotalStar() + string.Empty;
	}

	private void CountDown()
	{
		txtLeftTime.text = "剩余时间\n" + ActivityManager.instance.GetLeftTimeDesc();
		if (ActivityManager.instance.GetLeftTime() <= -60)
		{
			base.gameObject.SetActive(value: false);
		}
	}

	public void OpenPrizeWnd(int id)
	{
		ActivityPrize prize = ActivityManager.instance.GetPrize(id);
		for (int i = 0; i < _activityPrize.Length; i++)
		{
			_activityPrize[i].SetVisible(visible: false);
		}
		if (prize == null || string.IsNullOrEmpty(prize.items))
		{
			return;
		}
		prizeWnd.SetActive(value: true);
		if (id == 1)
		{
			GetPrizeCom(5).position = new Vector2(-2f, -13f);
		}
		else
		{
			GetPrizeCom(5).position = new Vector2(-4f, 131f);
		}
		bool flag = ActivityManager.instance.CanTakePrize(id);
		takePrizeWndBtn.SetActive(flag);
		prizeWndTxt.SetActive(!flag);
		_curSelectId = id;
		if (id == 5)
		{
			GetPrizeCom(100).SetVisible(visible: true);
		}
		string[] array = prize.items.Split(',');
		for (int j = 0; j < array.Length; j++)
		{
			string[] array2 = array[j].Split('_');
			int itemId = 0;
			int count = 0;
			if (array2.Length >= 1)
			{
				itemId = Convert.ToInt32(array2[0]);
			}
			if (array2.Length >= 2)
			{
				count = Convert.ToInt32(array2[1]);
			}
			UIActivityPrize prizeCom = GetPrizeCom(itemId);
			if (prizeCom != null)
			{
				prizeCom.SetVisible(visible: true);
				if (prizeCom.itemId != 100)
				{
					prizeCom.count = count;
				}
			}
		}
	}

	private UIActivityPrize GetPrizeCom(int itemId)
	{
		for (int i = 0; i < _activityPrize.Length; i++)
		{
			if (_activityPrize[i].itemId == itemId)
			{
				return _activityPrize[i];
			}
		}
		return null;
	}

	private UIActivityNode GetNode(int id)
	{
		for (int i = 0; i < _nodes.Count; i++)
		{
			if (_nodes[i].id == id)
			{
				return _nodes[i];
			}
		}
		return null;
	}

	public void TakePrize()
	{
		if (_curSelectId > 0)
		{
			ActivityManager.instance.TakePrize(_curSelectId);
			GetNode(_curSelectId).TakePrize();
			LaunchUIController.instance.RefreshActivityTip();
			if (ActivityManager.instance.IsTakenAllPrize())
			{
				base.gameObject.SetActive(value: false);
			}
		}
	}
}
